<?php

/**
 * 后台基础
 * @author 	xiaobai
 * @date 	2017-01-03
 */

namespace app\common\controller;
use think\Controller;
use think\facade\Request;

class Panelbase extends Controller
{

    public function initialize()
    {
        // session失效时重新写入
        if (empty(session('admin_id')) || session('expires_time') < time()) {
            session(null);
            $this->redirect("login/index");
        } else {
            session('expires_time', time()+2*3600);
        }

        // 获取左侧菜单列表
        $menu_model = model('Common/Menu');
        $menu_list_cache = $menu_model->menu_list_cache();
        $this->assign('menu_list_cache', $menu_list_cache);

        $module_name  = Request::module();
        $control_name = Request::controller();
        $action_name  = Request::action();
        $this->assign('module_name', $module_name);
        $this->assign('control_name', $control_name);
        $this->assign('action_name', $action_name);

        // 判断是否有清除缓存的权限
        $cache_row = $menu_model->get_menu_find_cache('panel', 'site', 'clear_cache');
        if ($this->check_access($cache_row)) {
            $this->assign('is_cache', 1);
        }

        $menu_parents = $menu_model->menu_parents($module_name, $control_name, $action_name);
        $this->assign("menu_parents",$menu_parents['ids']);
        $this->assign("bread_header",$menu_parents['names']);
        if (!$this->check_access($menu_parents['row'])) {
            $this->error("您没有访问权限！");
        }
    }

    // 统一列表
    public function index()
    {
        return $this->fetch();
    }

    // 统一添加
    public function add()
    {
        return $this->fetch();
    }

    // 统一添加执行保存
    public function add_post()
    {
        if (request()->isPost()) {
            $data = input('post.');

            // 数据验证
            if (!empty($this->validate_name)) {
                $result = $this->validate($data, $this->validate_name);
                if (true !== $result) {
                    $this->error($result);
                }
            }

            $url = !empty($this->url) ? $this->url : Url('index');
            if ($this->model->save($data)) {
                $this->success('保存成功', $url);
            } else {
                $this->error($this->model->getError());
            }
        } else {
            $this->error('请求错误');
        }
    }

    // 统一编辑
    public function edit()
    {
        $id  = input("param.id");
        $row = $this->model->where('id', $id)->find();
        if (empty($row)) {
            $this->error('数据不存在');
        }
        $row = $row->toArray();

        $this->assign($row);
        return $this->fetch();
    }

    // 统一编辑执行处理
    public function edit_post()
    {
        if (request()->isPost()) {
            $data = input('post.');
            // 数据验证
            if (!empty($this->validate_name)) {
                $result = $this->validate($data, $this->validate_name);
                if (true !== $result) {
                    $this->error($result);
                }
            }

            $url = !empty($this->url) ? $this->url : Url('index');
            if ($this->model->isUpdate(true)->save($data)) {
                $this->success('保存成功', $url);
            } else {
                $this->error($this->model->getError());
            }
        } else {
            $this->error('请求错误');
        }
    }

    // 统一排序，主键必须为id，排序键为sort
    public function listorders()
    {
        $post = input("post.");
        if (empty($post['sort']) || !is_array($post['sort'])) {
            $this->error('数据不能为空');
        }
        $data = array();
        foreach ($post['sort'] as $key=>$val) {
            $data[$key]['id'] = $key;
            $data[$key]['sort'] = $val;
        }
        $res = $this->model->saveAll($data);
        if ($res !== false) {
            $this->success('排序成功');
        } else {
            $this->error('排序失败');
        }
    }
    
    // 统一删除，只支持id单个删除，为物理删除
    public function delete()
    {
        $id = input("param.id", 0, 'intval');
        if (empty($id) || !is_numeric($id)) {
            $this->error('参数错误');
        }
        $control_array = array('Group');
        $control = request::instance()->controller();
        if (in_array($control, $control_array) && $id == 1) {
            $this->error('当前数据不支持删除');
        }
        $res = $this->model->where(array('id'=>$id))->delete();
        if ($res !== false) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 检查后台用户访问权限
     * @date   17-01-03
     * @author xiaobai
     * @param  $menus   [当前访问的菜单信息]
     * @return bool
     */
    private function check_access($menus)
    {
        $admin_id = session('admin_id');
        //如果用户角色是1，则无需判断
        if($admin_id == 1){
            return true;
        }

        $group_menuid = session('group_menuid');
        if( !in_array($menus['id'],$group_menuid) ){
            return false;
        }else{
            return true;
        }
    }

}
